package tcpMVMX;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;

public class ThreadM extends Thread {

    private Socket socket;

    public ThreadM(Socket socket) {
        this.socket = socket;
    }
    public ThreadM() {

    }

    public void run(){
        DataInputStream dataInputStream = null;
        try {
            //从端点socket得到一个字节流
            InputStream inputStream = socket.getInputStream();
            //把字节流包装成数据流
            dataInputStream = new DataInputStream(inputStream);
            while (true) {
                //使用数据流读取客户端数据
                String s = dataInputStream.readUTF();
                System.out.println(s);
                System.out.println(socket.getRemoteSocketAddress());
            }
        } catch (Exception e) {
            try {
                socket.close();
                if (dataInputStream != null) {
                    dataInputStream.close();
                }
                System.out.println("线程关闭了 " + socket.getRemoteSocketAddress());
            } catch (Exception ignored) {

            }
        }
    }

}
